package components.contorls.superClass
{
    import components.contorls.VScrollBar;
    import components.core.Container;
    import components.core.ScrollPolicy;
    import components.core.UIComponent;
    import components.events.ContainerSizeChangeEvent;
    import components.events.ScrollBarEvent;
    import components.utils.EventListenerUtil;

    import flash.display.DisplayObject;
    import flash.events.Event;

    import mx.events.ResizeEvent;

    public class BaseGroup extends UIComponent
    {
        private var _vScrollPolicy:String = "off";

        private var _hScrollPolicy:String = "off";

        private var _vGap:Number = 4;

        private var _hGap:Number = 4;

        private var _showBackground:Boolean = true;

        public function BaseGroup()
        {
            super();

            EventListenerUtil.addEventListener(vScrollBar, ScrollBarEvent.VISIBLE_CHANGE, scrollBar_visibleChangeHanlder);
            EventListenerUtil.addEventListener(hScrollBar, ScrollBarEvent.VISIBLE_CHANGE, scrollBar_visibleChangeHanlder);
            EventListenerUtil.addEventListener(container, ResizeEvent.RESIZE, container_resizeHandler);
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get container():Container
        {
            return super.getChildByName("groupSkin") as Container;
        }

        public function get vScrollBar():BaseVScrollBar
        {
            return super.getChildByName("vScrollBarSkin") as BaseVScrollBar;
        }

        public function get hScrollBar():BaseHScrollBar
        {
            return super.getChildByName("hScrollBarSkin") as BaseHScrollBar;
        }

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
                vScrollBar.top = 0;
                vScrollBar.right = 0;

                hScrollBar.left = 0;
                hScrollBar.bottom = 0;

                container.left = 0;
                container.top = 0;
                setPartSize();
            }

            container.showBackground = showBackground;
			
			if(_layout)
			{
				_layout.hGap=hGap;
				_layout.vGap=vGap;
				_layout.container=container;
			}
        }

        protected override function measureSizes():Boolean
        {
            var change:Boolean = super.measureSizes();
            return change;
        }

        protected override function updateDisplayList():void
        {
            super.updateDisplayList();

            vScrollBar.scrollPolicy = vScrollPolicy;
            hScrollBar.scrollPolicy = hScrollPolicy;
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        [Inspectable(type = "List", enumeration = "auto,on,off", defaultValue = "off")]
        public function get vScrollPolicy():String
        {
            return _vScrollPolicy;
        }

        public function set vScrollPolicy(value:String):void
        {
            _vScrollPolicy = value;
            invalidate();
        }

        [Inspectable(type = "List", enumeration = "auto,on,off", defaultValue = "off")]
        public function get hScrollPolicy():String
        {
            return _hScrollPolicy;
        }

        public function set hScrollPolicy(value:String):void
        {
            _hScrollPolicy = value;
            invalidate();
        }

        [Inspectable(defaultValue = "4")]
        public function get vGap():Number
        {
            return _vGap;
        }

        public function set vGap(value:Number):void
        {
            _vGap = value;
            invalidate();
        }

        [Inspectable(defaultValue = "4")]
        public function get hGap():Number
        {
            return _hGap;
        }

        public function set hGap(value:Number):void
        {
            _hGap = value;
            invalidate();
        }

        [Inspectable(defaultValue = "true")]
        public function get showBackground():Boolean
        {
            return _showBackground;
        }

        public function set showBackground(value:Boolean):void
        {
            _showBackground = value;
            invalidate();
        }

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public override function addChild(child:DisplayObject):DisplayObject
        {
            container.addChild(child);

            invalidate();
            return child;
        }

        public override function addChildAt(child:DisplayObject, index:int):DisplayObject
        {
            container.addChildAt(child, index);

            invalidate();
            return child;
        }

        public override function removeChild(child:DisplayObject):DisplayObject
        {
            container.removeChild(child);

            invalidate();
            return child;
        }

        public override function removeChildAt(index:int):DisplayObject
        {
            var child:DisplayObject = container.removeChildAt(index);

            invalidate();
            return child;
        }

        override public function getChildAt(index:int):DisplayObject
        {
            return container.getChildAt(index);
        }


        public override function getChildIndex(child:DisplayObject):int
        {
            return container.getChildIndex(child);
        }

        override public function get numChildren():int
        {
            return container.numChildren;
        }

        public override function getChildByName(name:String):DisplayObject
        {
            var child:DisplayObject;

            child = container.getChildByName(name);
            if (!child)
                child = super.getChildByName(name);

            return child;
        }

        protected function scrollBar_visibleChangeHanlder(event:Event):void
        {
            setPartSize();
        }

        private function setPartSize():void
        {
            if (vScrollBar.scrollPolicy == ScrollPolicy.AUTO)
                hScrollBar.right = container.right = vScrollBar.visible ? vScrollBar.width : 0;
            else if (vScrollBar.scrollPolicy == ScrollPolicy.ON)
                hScrollBar.right = container.right = vScrollBar.width;
            else
                hScrollBar.right = container.right = 0;

            if (hScrollBar.scrollPolicy == ScrollPolicy.AUTO)
                vScrollBar.bottom = container.bottom = hScrollBar.visible ? hScrollBar.height : 0;
            else if (hScrollBar.scrollPolicy == ScrollPolicy.ON)
                vScrollBar.bottom = container.bottom = hScrollBar.height;
            else
                vScrollBar.bottom = container.bottom = 0;
        }

        public override function set focusComp(value:UIComponent):void
        {
            super.focusComp = value;

            vScrollBar.focusComp = focusComp;
            hScrollBar.focusComp = focusComp;
            container.focusComp = focusComp;
        }

        public override function destroy():void
        {
            super.destroy();
            _vScrollPolicy = null;
            _hScrollPolicy = null;
        }

        public function get contentWidth():Number
        {
            return container.contentWidth;
        }

        public function get contentHeight():Number
        {
            return container.contentHeight;
        }

        protected function container_resizeHandler(event:ResizeEvent):void
        {
            dispatchEvent(new ContainerSizeChangeEvent());
        }
    }
}
